package com.alibaba.provider;

import com.alibaba.dubbo.api.DemoService;
import com.alibaba.dubbo.common.compiler.support.ClassUtils;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.remoting.Transporter;
import com.alibaba.dubbo.rpc.Protocol;
import com.alibaba.dubbo.util.DubboConfig;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Provider {
 
    public static void main(String[] args) throws Exception {
//        startByCode();
        startBySpring();
//        check();
    }

    public static void startBySpring() throws Exception{
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
        context.start();
        System.out.println("provider start......");
        check();
        System.in.read(); // 按任意键退出
    }

    public static void startByCode() throws Exception{
        ApplicationConfig applicationConfig=new ApplicationConfig();
        applicationConfig.setName("my-hello-word-app-provider");

        String zkAddress= DubboConfig.getZkUrl();
        List<RegistryConfig> registryConfigList=new ArrayList<RegistryConfig>();
        RegistryConfig registryConfig=new RegistryConfig();
        registryConfig.setAddress(zkAddress);
        registryConfig.setProtocol("zookeeper");
        registryConfigList.add(registryConfig);

        List<ProtocolConfig> protocolConfigList=new ArrayList<ProtocolConfig>();
        ProtocolConfig protocolConfig=new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20880);
        protocolConfig.setAccesslog("true");
//        protocolConfig.setTransporter("netty4");
//        protocolConfig.setSerialization("kryo");
        protocolConfigList.add(protocolConfig);

        DemoService demoService=new DemoServiceImpl();
        ServiceConfig serviceConfig=new ServiceConfig();
        serviceConfig.setRef(demoService);
        serviceConfig.setProtocols(protocolConfigList);
        serviceConfig.setRegistries(registryConfigList);
        serviceConfig.setApplication(applicationConfig);
        serviceConfig.setInterface(DemoService.class);
        serviceConfig.export();
        System.out.println("provider start........");
        System.in.read();
    }

    public static void checkURI(){
        String uri="dubbo://172.16.4.34:9090/com.nonobank.finance.api.solr.FinanceLogSolrApi?accepts=1000&anyhost=true&application=finance-consumer&buffer=8192&charset=UTF-8&check=false&client=netty&codec=dubbo&default.timeout=30000&dubbo=2.4.9&interface=com.nonobank.finance.api.solr.FinanceLogSolrApi&iothreads=9&methods=find,findBySolrQuery&payload=8388608&pid=15088&revision=3.0.0-SNAPSHOT&serialization=hessian2&server=netty&side=consumer&timestamp=1452241284033,";
    }

    public static void check(){
        Transporter transporter= ExtensionLoader.getExtensionLoader(Transporter.class).getAdaptiveExtension();
        com.alibaba.dubbo.remoting.Transporter extension =
                (com.alibaba.dubbo.remoting.Transporter)ExtensionLoader
                        .getExtensionLoader(com.alibaba.dubbo.remoting.Transporter.class).getExtension("netty");

        System.out.println();
    }
}